(function() {
  var express, iced, moment, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  moment = require('moment');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, end_date, err, result, start_date, table_name, today, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    where_clause = "(\n    r.uuid like $1 or\n    case\n    when t1.chnl in (1,2,4) then\n        json_extract_path_text(t1.i_pkt, '42') like $1 or\n        json_extract_path_text(t1.i_pkt, '41') like $1 or\n        json_extract_path_text(t1.i_pkt, '4')  like $1 or\n        json_extract_path_text(t1.i_pkt, '11') like $1\n    end\n)";
    today = moment().format('YYYY-MM-DD');
    start_date = req.body.start_date || req.query.start_date || today;
    end_date = req.body.end_date || req.query.end_date || today;
    table_name = 'pay_trlist_hist';
    if (start_date === today && end_date === today) {
      table_name = 'pay_trlist';
    }
    where_clause += " and r.ctime::date >= '" + start_date + "'";
    res.locals.start_date = start_date;
    where_clause += " and r.ctime::date <= '" + end_date + "'";
    res.locals.end_date = end_date;
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/t40_recvr/done.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 39
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/t40_recvr/done.coffee"
          });
          client.query("select count(*)::int as count from pay_t40_recvr as r left join " + table_name + " as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true where " + where_clause + " and r.checked = true", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 48
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/t40_recvr/done.coffee"
            });
            client.query("select r.*, t1.sys_time as tr1_systime, json_extract_path_text(t1.merch, 'name') as tr1_mername, case when t1.chnl in (1,2,4) then array[ json_extract_path_text(t1.i_pkt, '42'), json_extract_path_text(t1.i_pkt, '41'), json_extract_path_text(t1.i_pkt, '4'), json_extract_path_text(t1.i_pkt, '11')] end as tr1_info, u.realname as input_user, u2.realname as check_user from pay_t40_recvr as r left join " + table_name + " as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true left join web_users as u on u.uuid = r.inputer left join web_users as u2 on u2.uuid = r.checker where " + where_clause + " and r.checked = true order by r.serial desc offset $2 limit $3", ['%' + res.locals.keyword + '%', res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 73
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            res.locals.records = result.rows;
            return res.render('t40_recvr/done');
          });
        });
      };
    })(this));
  });

}).call(this);
